package cn.ht.ThreadPoolDemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 第四种获得/使用java多线程的方式 -- 线程池
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {
        //一池5个处理线程（大堂有5个工作窗口）
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //一池一线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //一池N线程，视情况而定
        ExecutorService threadPool = Executors.newCachedThreadPool();

        //上述底层都是ThreadPoolExecutor

        //模拟10个用户来办理业务，每个用户都是一个来自外部的请求线程
        try {

            for (int i = 1; i <= 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }

    }

}
